
# coding:utf-8

from lib import flux, al, smokesignal
from lib.fluxext import singleton

from base import config
from views.player import Player
from views.enemy import Enemy
from system.game import Game
from system import phy

from utils.suger import *

@singleton
class ScreenGame(flux.Screen):

    def AfterUpdate(self, dt):
        phy.Update(dt)

    def OnInit(self):

        Game().init(self)
        Game().map.load('data/maps/1.fmap')

        self.player = Player()
        self.player.AddToScreen(self)
        self.player.anchor.SetAnchor(Game().map.map_top_left)

        self.player.SetMapPos(18,15)
        self.player.ApplyPhy()
        Game().bind_player(self.player)

        e = Enemy()
        e.AddToScreen(self)
        e.SetMapPos(18,9)
        e.ApplyPhy()
        Game().unit.add(e)

        self.RegKey(al.KEY_SPACE)
        self.RegKey(al.KEY_Q)
        self.RegKey(al.KEY_W)
        self.RegKey(al.KEY_E)
        self.RegKey(al.KEY_R)
        self.RegKey(al.KEY_T)
        self.RegKey(al.KEY_LEFT)
        self.RegKey(al.KEY_RIGHT)
        self.RegKey(al.KEY_UP)
        self.RegKey(al.KEY_DOWN)
        self.RegKey(al.KEY_TAB)

        self._k = None

    def OnFocus(self):
        smokesignal.emit('game_continue')
        theWorld.SetPause(False)
        self.player.PauseCheck()
        Game().log.add('游戏继续')

    def OnLostFocus(self):
        smokesignal.emit('game_pause')
        theWorld.SetPause(True)
        Game().log.add('游戏暂停')

    def OnMouseInput(self, action, x, y, button):
        _ = False
        skltip = Game().ui.skltip
        for k,v in Game().ui.skl:
            if v.skl and v.PtInView(x,y):
                _ = True
                if not skltip.GetVisible() or self._k != k:
                    skltip.SetText(v.skl.get_text())
                    skltip.SetPos(x, y)
                    self._k = k
                else:
                    skltip.SetPos(x, y)

        if not _ and button:
            #Game().log.add('%d %d %d %d' % (action, x, y, button))
            print action, x, y, button
        skltip.SetVisible(_)
        #print action,x,y,button

    def OnKeyInput(self, action, key, mods):
        #print action, key, mods
        key2anim = {
            al.KEY_LEFT  : 'left',
            al.KEY_RIGHT : 'right',
            al.KEY_UP    : 'up',
            al.KEY_DOWN  : 'down',
        }

        key2skl = {
            al.KEY_Q  : 0,
            al.KEY_W  : 1,
            al.KEY_E  : 2,
            al.KEY_R  : 3,
            al.KEY_T  : 4,
        }

        if action == al.EVENT_KEY_DOWN:
            if key == al.KEY_SPACE:
                self.player.Attack()
            elif key == al.KEY_TAB:
                #Game().ui.lock.at(Game().unit.nameless[0])
                Game().ui.lock.focus()
            elif key in key2skl.keys():
                smokesignal.emit('btn_spell_cast', key2skl[key])

        for k,v in key2anim.items():
            if key == k:
                if action == al.EVENT_KEY_DOWN:
                    Game().player_move(key)
                elif action == al.EVENT_KEY_UP:
                    Game().player_un_move(key)
